﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Net.Chirash.Twitter.TwitterClient
{
    /// <summary>
    /// 例外からエラーメッセージを生成する機能を提供します。
    /// </summary>
    public class ErrorMessageProvider
        : INotifyPropertyChanged
    {

        #region Constructor

        public ErrorMessageProvider()
        {
        }

        public ErrorMessageProvider(Exception ex)
            : this()
        {
            this.TargetException = ex;
        }

        #endregion

        #region public Exception TargetException { get; set; }
        private Exception _TargetException { get; set; }
        public Exception TargetException
        {
            get { return _TargetException; }
            set { _TargetException = value; OnPropertyChanged("TargetException"); }
        }
        #endregion

        /// <summary>
        /// TargetExceptionを基に、エラーメッセージを生成します。
        /// </summary>
        /// <returns>生成されたメッセージ</returns>
        public string CreateErrorMessage()
        {
            return CreateErrorMessage(TargetException);
        }

        /// <summary>
        /// 指定された例外を基に、エラーメッセージを生成します。
        /// </summary>
        /// <param name="ex">メッセージを生成する例外</param>
        /// <returns>生成されたメッセージ</returns>
        public static string CreateErrorMessage(Exception ex)
        {
            if (ex == null)
                return "";

            string result = "";
            if (ex is System.Net.WebException)
            {
                var netex = ex as System.Net.WebException;
                if (netex.Response != null)
                {
                    switch ((netex.Response as System.Net.HttpWebResponse).StatusCode)
                    {
                        case System.Net.HttpStatusCode.Forbidden:
                            result = "Forbidden(403) : I'm sorry. You may be blocked from this user.";
                            break;
                        case System.Net.HttpStatusCode.BadRequest:
                            result = "BadRequset(400) : Sorry, I called Twitter API so many times.";
                            break;
                        case System.Net.HttpStatusCode.Unauthorized:
                            result = "Unauthorized(401) : This user may lock his tweets.";
                            break;
                        case System.Net.HttpStatusCode.OK:
                            result = "Success(200) : There is no problem.";
                            break;
                        case System.Net.HttpStatusCode.InternalServerError:
                            result = "InternalServerError(500) : Some accidents occured in the Twitter server.";
                            break;
                        case System.Net.HttpStatusCode.BadGateway:
                            result = "BadGateWay(502) : Now Twitter server is under inspection or stopped.";
                            break;
                        case System.Net.HttpStatusCode.ServiceUnavailable:
                            result = "ServiceUnavailable(503) : Try access later after a moment";
                            break;
                    }
                }
                else
                {
                    result = "There is no response.";
                }
            }
            else
            {
                result = ex.Message;

                Console.WriteLine(ex.ToString());
            }
#if DEBUG
            if(ex is ApplicationException)
            {
                result = "Debuging Exception!";
            }
#endif
            return result;
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion

    }
}
